<?php

/**
 * 行为分析-每天访问汇总数据
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActDayDataVisitController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_ActDayDataVisit();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['version'] = trim($this->_request->get('version'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['sourceSubId'] = trim($this->_request->get('sourceSubId'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['osName'] != '') {
            $wheres[] = "`osNameFlag` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['version'] != '') {
            $wheres[] = "`version` = '" . addslashes($vars['version']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }
        if ($vars['sourceSubId'] != '') {
            $wheres[] = "`sourceSubId` = '" . addslashes($vars['sourceSubId']) . "'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        
        $wheres[] ="`date`>='2011-09-24'";
        $orderBy = "`visitNum` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT userSign,udid,productCode,osNameFlag,version,sourceId,sourceSubId,sum(visitNum) as visitNum,sum(visitLength) as visitLength FROM `{$this->_model->getTableName()}` {$where} GROUP BY userSign,udid,productCode,osNameFlag,version,sourceId,sourceSubId ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //列出操作系统
        $osNameRows = $this ->_model ->getAdapter()->fetchAll("SELECT `osNameFlag` FROM `{$this->_model->getTableName()}` GROUP BY `osNameFlag` ORDER BY `osNameFlag`");
        $this->view->osNames = $osNameRows;
    }
     /**
     * 按日期
     * 
     * @return void
     */
    public function byDateAction()
    {
        $time = time();
        $cookieModel = new Application_Model_Cookie();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['version'] = trim($this->_request->get('version'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['sourceSubId'] = trim($this->_request->get('sourceSubId'));
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $vars['userSign'] = trim($this->_request->get('userSign'));
        $vars['backUrl'] = urlencode(trim($this->_request->get('backUrl')));
        $urls = $vars;

        $wheres[] = "`productCode` = '". Application_Model_UserLogin::getInstance()->getUserCurrentProductCode() ."'";
        if ($vars['osName'] != '') {
            $wheres[] = "`osNameFlag` = '" . addslashes($vars['osName']) . "'";
        }
        if ($vars['version'] != '') {
            $wheres[] = "`version` = '" . addslashes($vars['version']) . "'";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '" . addslashes($vars['sourceId']) . "'";
        }
        if ($vars['sourceSubId'] != '') {
            $wheres[] = "`sourceSubId` = '" . addslashes($vars['sourceSubId']) . "'";
        }
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        if ($vars['userSign'] != '') {
            $wheres[] = "`userSign` = '" . addslashes($vars['userSign']) . "'";
        }
        
        $wheres[] ="`date`>='2011-09-24'";
        $orderBy = "`date` DESC";

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT userSign,udid,productCode,osNameFlag,version,sourceId,sourceSubId,date,sum(visitNum) as visitNum,sum(visitLength) as visitLength FROM `{$this->_model->getTableName()}` {$where} GROUP BY userSign,udid,productCode,osNameFlag,version,sourceId,sourceSubId,date ORDER BY {$orderBy}";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;

        $rows = $dbPaginator->getRows();

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
        
        //列出操作系统
        $osNameRows = $this ->_model ->getAdapter()->fetchAll("SELECT `osNameFlag` FROM `{$this->_model->getTableName()}` GROUP BY `osNameFlag` ORDER BY `osNameFlag`");
        $this->view->osNames = $osNameRows;
    }

    /**
     * 获取APP版本号
     * @return json string
     */
    public function appversionAction() 
    {
        $osName = trim($this->_request->getPost('osName'));
        $rows = $this ->_model ->getAdapter()->fetchAll("SELECT `version` FROM `{$this->_model->getTableName()}` WHERE `osNameFlag`='".$osName."' GROUP BY `version` ORDER BY `version`");
        $res = array();
        foreach($rows as $row) {
            $res[]=$row['version'];
        }
        $json_string=json_encode($res);
        echo $json_string;
        exit();
        
    }
}